﻿using Sys_PersonnelManagement.BLL;
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sys_PersonnelManagement.Web
{
    public partial class Main : System.Web.UI.Page
    {
        //项目说明：当登录账号(FUserNum)以1开头是员工，以2开头是管理员，以3开头是部门主管
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断当前用户是否登录
            if (Session["UserID"] == null)
            {
                //如果登录失败，回到登录界面
                Response.Redirect("~/Web/Login.aspx");
            }
            else
            {
                lbl_User.Text = ActionDB.UserNameSelect((int)Session["UserID"]);
                Session["admin"] = ActionDB.UserNumSelect((int)Session["UserID"]);
                GvDataBind();
            }
        }
        //退出登录按钮
        protected void btn_UserExit_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("~/Web/Login.aspx");
        }
        //gv表赋值
        private void GvDataBind()
        {
            //员工管理
            gv_yggl.DataSource = ActionDB.yggl();
            gv_yggl.DataBind();
            //部门管理
            gv_depart.DataSource = ActionDB.depart();
            gv_depart.DataBind();
            //休假管理
            gv_vacation.DataSource = ActionDB.vacaSelect();
            gv_vacation.DataBind();
            //事务日志
            gv_log.DataSource = ActionDB.logSelect((int)Session["UserID"]);
            gv_log.DataBind();
            //员工考勤
            gv_UserWork.DataSource = ActionDB.userWork((int)Session["UserID"]);
            gv_UserWork.DataBind();
            //加班管理
            gv_Overtime.DataSource = ActionDB.OvertimeSelect((int)Session["UserID"]);
            gv_Overtime.DataBind();
            //工资管理
            if ((int)Session["admin"] == 2)
            {
                //管理员
                gv_Wage.DataSource = ActionDB.wageAdmin();
                gv_Wage.DataBind();
            }
            else
            {
                //用户和部门主管权限
                gv_Wage.DataSource = ActionDB.wage((int)Session["UserID"]);
                gv_Wage.DataBind();
            }
            //历史记录
            gv_HistoryWage.DataSource = ActionDB.historyWage((int)Session["UserID"]);
            gv_HistoryWage.DataBind();
        }
        //隐藏所有的panel
        private void panVis()
        {
            //通过foreach遍历所有的窗体控件
            foreach (Control item in form1.Controls)
            {
                //如果是panel，让所有panel隐藏
                if (item.GetType().ToString().Equals("System.Web.UI.WebControls.Panel"))
                {
                    item.Visible = false;
                }
            }
        }
        
        //首页
        protected void btn_home_Click(object sender, EventArgs e)
        {
            panVis();
            pan_main.Visible = true;
        }
        //员工管理
        protected void btn_yggl_Click(object sender, EventArgs e)
        {
            //管理员
            if ((int)Session["admin"] == 2 || (int)Session["admin"] == 3)
            {
                panVis();
                pan_yggl.Visible = true;
                GvDataBind();
            }
            else
            {
                Response.Write("<script>alert('您没有该权限！')</script>");
            }
        }
        //部门管理
        protected void btn_bmgl_Click(object sender, EventArgs e)
        {
            if ((int)Session["admin"] == 2 )
            {
                panVis();
                pan_bmgl.Visible = true;
                GvDataBind();
            }
            else
            {
                Response.Write("<script>alert('您没有该权限！')</script>");
            }
        }
        //休假管理
        protected void btn_xjgl_Click(object sender, EventArgs e)
        {
            if ((int)Session["admin"] == 2 || (int)Session["admin"] == 3)
            {
                panVis();
                pan_xjgl.Visible = true;
                GvDataBind();
            }
            else
            {
                Response.Write("<script>alert('您没有该权限！')</script>");
            }
        }
        //事务日志
        protected void btn_swrz_Click(object sender, EventArgs e)
        {
            panVis();
            pan_swrz.Visible = true;
            GvDataBind();
        }
        //人事考勤
        protected void btn_rskq_Click(object sender, EventArgs e)
        {
            panVis();
            pan_rskq.Visible = true;
            GvDataBind();
        }
        //加班管理
        protected void btn_jbgl_Click(object sender, EventArgs e)
        {

            panVis();
            pan_jbgl.Visible = true;
            GvDataBind();
        }
        //工资管理
        protected void btn_gzgl_Click(object sender, EventArgs e)
        {
            panVis();
            pan_gzgl.Visible = true;
            GvDataBind();
        }
        //员工管理 再次确认是否删除 Confirm
        protected void gv_yggl_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[10].Attributes.Add("onclick", "return confirm('你确认要删除吗?')");
            }
        }

        //员工查询返回按钮
        protected void btn_Hui_Click(object sender, EventArgs e)
        {
            panVis();
            pan_yggl.Visible = true;
        }
        //员工查询显示按钮
        protected void btn_UserSearch_Click(object sender, EventArgs e)
        {
            gv_Search.DataSource = ActionDB.ygglSearch(txt_UserSearch.Text);
            gv_Search.DataBind();
            panVis();
            pan_UserSearch.Visible = true;
        }
       
        //员工管理 删除功能
        protected void gv_yggl_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if(ActionDB.ygglDelete((int)Session["UserID"], int.Parse(gv_yggl.Rows[e.RowIndex].Cells[0].Text)))
            {
                Response.Write("<script>alert('删除成功')</script>");
                GvDataBind();
            }
            else
            {
                Response.Write("<script>alert('删除失败')</script>");
            }
        }
        //员工管理 添加功能
        protected void btn_UserAdd_Click(object sender, EventArgs e)
        {
            panVis();
            pan_UserAdd.Visible = true;
            btn_UserAdd.Visible = true;
        }
        //员工管理 确定添加功能
        protected void btn_yggladdsubmit_Click(object sender, EventArgs e)
        {
            try
            {
                ActionDB.ygglAdd((int)Session["UserID"], txt_addUserCode.Text, txt_addname.Text, int.Parse(txt_addSex.Text), int.Parse(txt_addUserAge.Text), txt_UserNum.Text, txt_AddPwd.Text, txt_AddPhone.Text, txt_emil0.Text, int.Parse(ddl_bm.SelectedValue));
                panVis();
                pan_yggl.Visible = true;
                GvDataBind();
                btn_UserAdd.Visible = true;
            }
            catch (Exception)
            {
                Response.Write("<script>alert('添加失败')</script>");
            }
        }
        //员工管理 取消添加功能
        protected void btn_UserAddExit_Click(object sender, EventArgs e)
        {
            panVis();
            pan_yggl.Visible = true;
            btn_UserAdd.Visible = true;
        }
        //员工管理 修改功能
        protected void gv_yggl_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            panVis();
            pan_ygglUpdate.Visible = true;
            //根据ID查询出数据填入表单中
            Session["ID"] = int.Parse(gv_yggl.Rows[e.RowIndex].Cells[0].Text);
            DataTable td = ActionDB.ygglselect((int)Session["ID"]);
            txt_upname.Text = td.Rows[0][2].ToString();
            txt_upsex.Text = td.Rows[0][3].ToString();
            txt_upage.Text = td.Rows[0][4].ToString();
            txt_uppwd.Text = td.Rows[0][6].ToString();
            txt_upphone.Text = td.Rows[0][7].ToString();
            txt_emil.Text = td.Rows[0][8].ToString();

        }
        //员工管理 确认修改功能
        protected void btn_ygglupsubmit_Click(object sender, EventArgs e)
        {
            //执行修改操作
            if (ActionDB.ygglUpdate((int)Session["UserID"], txt_upname.Text, int.Parse(txt_upsex.Text), int.Parse(txt_upage.Text), txt_uppwd.Text, txt_upphone.Text, txt_emil.Text, int.Parse(ddl_updm.SelectedValue), (int)Session["ID"]))
            {
                panVis();
                GvDataBind();
                pan_yggl.Visible = true;
            }
            else
            {
                Response.Write("<script>alert('修改失败')</script>");
            }
        }
        //员工管理 取消修改功能
        protected void btn_Upexit_Click(object sender, EventArgs e)
        {
            panVis();
            pan_yggl.Visible = true;
        }
        //部门管理 再次确认是否删除 Confirm
        protected void gv_depart_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[4].Attributes.Add("onclick", "return confirm('你确认要删除吗?')");
            }
        }
        //部门查找
        protected void btn_dSearch_Click(object sender, EventArgs e)
        {
            gv_deSearch.DataSource = ActionDB.departSearch(int.Parse(txt_dSearch.Text));
            gv_deSearch.DataBind();
            panVis();
            pan_departSearch.Visible = true;
        }
        //部门查找返回
        protected void btn_dHui_Click(object sender, EventArgs e)
        {
            panVis();
            pan_bmgl.Visible = true;
        }
        //部门管理修改按钮
        protected void gv_depart_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            panVis();
            pan_deptUpdate.Visible = true;
            txtDeptID.Text = gv_depart.Rows[e.RowIndex].Cells[0].Text;
            txtDept.Text = gv_depart.Rows[e.RowIndex].Cells[1].Text;
            txtUp.Text = gv_depart.Rows[e.RowIndex].Cells[2].Text;
            txtDeptH.Text = gv_depart.Rows[e.RowIndex].Cells[3].Text;
        }
        //添加部门确定按钮
        protected void btn_departSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                ActionDB.departAdd((int)Session["UserID"], txtDeptName.Text, int.Parse(txtUpDept.Text), int.Parse(txtDeptHead.Text));
                panVis();
                GvDataBind();
                pan_bmgl.Visible = true;
                btn_departAdd.Visible = true;
            }
            catch (Exception)
            {
                Response.Write("<script>alert('添加失败')</script>");
            }
        }
        //“添加部门”按钮点击事件，显示添加部门页面
        protected void btn_departAdd_Click(object sender, EventArgs e)
        {
            panVis();
            pan_departAdd.Visible = true;
            btn_departAdd.Visible = true;
        }
        //添加部门返回按钮
        protected void btn_departAddExit_Click(object sender, EventArgs e)
        {
            panVis();
            pan_bmgl.Visible = true;
            btn_departAdd.Visible = true;
        }
        //修改部门提交操作
        protected void btn_deptSubmit_Click(object sender, EventArgs e)
        {
            //执行修改操作
            //departUpdate(int DeptID, string DeptName, int ParentID, int DeptHeadID)
            if (ActionDB.departUpdate((int)Session["UserID"], int.Parse(txtDeptID.Text), txtDept.Text, int.Parse(txtUp.Text), int.Parse(txtDeptH.Text)))
            {
                panVis();
                GvDataBind();
                pan_bmgl.Visible = true;
            }
            else
            {
                Response.Write("<script>alert('修改失败')</script>");
            }
        }
        //修改部门返回操作
        protected void btn_deptReturn_Click(object sender, EventArgs e)
        {
            panVis();
            pan_bmgl.Visible = true;
        }
        //删除部门操作
        protected void gv_depart_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (ActionDB.departDelete((int)Session["UserID"], int.Parse(gv_depart.Rows[e.RowIndex].Cells[0].Text)))
            {
                Response.Write("<script>alert('删除成功')</script>");
                GvDataBind();
            }
            else
            {
                Response.Write("<script>alert('删除失败')</script>");
            }
        }
        //审批按钮点击事件
        protected void btn_pass_Click(object sender, EventArgs e)
        {
            if ((int)Session["admin"] == 2 || (int)Session["admin"] == 3)
            {
                panVis();
                pan_pass.Visible = true;
                btn_pass.Visible = false;
            }
            else
            {
                Response.Write("<script>alert('您没有该权限')</script>");
            }

        }
        //确认审批界面操作
        protected void btnPassConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtMark.Text == "".Trim())
                {
                    Response.Write("<script>alert('请填写备注')</script>");
                }
                else
                {
                    ActionDB.vacationAdd((int)Session["UserID"], int.Parse(txtName.Text), DateTime.Parse(txtStart.Text), DateTime.Parse(txtEnd.Text), int.Parse(txtDays.Text), int.Parse(ddl_sp.SelectedValue), txtMark.Text);
                    panVis();
                    pan_xjgl.Visible = true;
                    Page_Load(null, null);
                    btn_pass.Visible = true;
                    gv_vacation.DataSource = ActionDB.vacaSelect();
                    gv_vacation.DataBind();
                }
            }
            catch (Exception ex)
            {
                string str = "<script>alert('{0}')</script>";
                str = string.Format(str, ex.ToString());
                Response.Write(str) ;
            }
        }
        //取消审批操作
        protected void btnPassExit_Click(object sender, EventArgs e)
        {
            panVis();
            btn_pass.Visible = true;
            pan_xjgl.Visible = true;
        }

        //上班打卡按钮
        protected void btn_sta_Click(object sender, EventArgs e)
        {
            if (ActionDB.userWorkAdd((int)Session["UserID"], DateTime.Now))
            {
                Response.Write("<script>alert('签到成功！')</script>");
                GvDataBind();
                btn_end.Visible = true;
                btn_sta.Visible = false;
            }
            else
            {
                Response.Write("<script>alert('签到失败！')</script>");
            }

        }
        //下班按钮
        protected void btn_end_Click(object sender, EventArgs e)
        {
            if (ActionDB.userWorkUpdate(int.Parse(gv_UserWork.Rows[gv_UserWork.Rows.Count - 1].Cells[0].Text), (int)Session["UserID"]))
            {
                Response.Write("<script>alert('下班成功！')</script>");
                GvDataBind();
                btn_sta.Visible = true;
                btn_end.Visible = false;
            }
            else
            {
                Response.Write("<script>alert('下班失败！')</script>");
            }
        }
        //加班申请点击事件
        protected void btn_OvertimeAdd_Click(object sender, EventArgs e)
        {
            panVis();
            pan_OvAdd.Visible = true;
        }
        //加班申请表单填写完的确认按钮
        protected void btn_Ovsubmit_Click(object sender, EventArgs e)
        {
            if (ActionDB.OvertimeAdd((int)Session["UserID"], DateTime.Parse(txt_ovSta.Text), DateTime.Parse(txt_ovEdn.Text), int.Parse(txt_ovDeys.Text), txt_Remark.Text))
            {
                if (txt_Remark.Text == "".Trim())
                {
                    Response.Write("<script>alert('请填写备注')</script>");
                }
                else
                {
                    Response.Write("<script>alert('申请成功！')</script>");
                    GvDataBind();
                    panVis();
                    pan_jbgl.Visible = true;
                }
            }
            else
            {
                Response.Write("<script>alert('申请失败！')</script>");
            }
        }
        //加班申请表单返回按钮
        protected void btn_Ovexit_Click(object sender, EventArgs e)
        {
            panVis();
            pan_jbgl.Visible = true;
        }
        //加班审批赋值
        protected void gv_Overtime_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            panVis();
            pan_OvUpdate.Visible = true;
            lbl_OvSat.Text = gv_Overtime.Rows[e.RowIndex].Cells[2].Text;
            lbl_OvEnd.Text = gv_Overtime.Rows[e.RowIndex].Cells[3].Text;
            lbl_OvDeys.Text = gv_Overtime.Rows[e.RowIndex].Cells[4].Text;
            lbl_Remark.Text = gv_Overtime.Rows[e.RowIndex].Cells[6].Text;
            Session["OvID"] = gv_Overtime.Rows[e.RowIndex].Cells[0].Text;
        }
        //加班审批
        protected void btn_OvupdateSub_Click(object sender, EventArgs e)
        {
            if (ActionDB.OvertimeUpdate(int.Parse(ddl_OvStates.SelectedValue), int.Parse(Session["OvID"].ToString()), (int)Session["UserID"]))
            {
                Response.Write("<script>alert('审批成功！')</script>");
                GvDataBind();
                panVis();
                pan_jbgl.Visible = true;
            }
            else
            {
                Response.Write("<script>alert('审批失败！')</script>");
            }
        }
        //加班审批返回
        protected void btn_OvUpdateExit_Click(object sender, EventArgs e)
        {
            panVis();
            pan_jbgl.Visible = true;
        }
        //设置薪资
        protected void btn_money_Click(object sender, EventArgs e)
        {
            if (ActionDB.WageSelect((int)Session["UserID"]))
            {
                pan_WageUpdate.Visible = true;
                txt_moneyUpdate.Text = gv_Wage.Rows[0].Cells[1].Text;
            }
            else
            {
                pan_WageAdd.Visible = true;
            }
        }
        //历史薪资按钮
        protected void btn_HistoryWage_Click(object sender, EventArgs e)
        {
            panVis();
            pan_HistoryWage.Visible = true;
        }
        //设置薪资添加
        protected void btn_WageAddsubmit_Click(object sender, EventArgs e)
        {
            if (ActionDB.WageAdd((int)Session["UserID"], float.Parse(txt_mondyAdd.Text)))
            {
                Response.Write("<script>alert('设置成功！')</script>");
                GvDataBind();
                pan_WageAdd.Visible = false;
            }
            else
            {
                Response.Write("<script>alert('设置失败！')</script>");
            }
        }
        //设置薪资添加返回
        protected void btn_WageAddExit_Click(object sender, EventArgs e)
        {
            pan_WageAdd.Visible = false;
        }
        //设置薪资修改
        protected void btn_WageUpdateSub_Click(object sender, EventArgs e)
        {
            if (ActionDB.WageUpdate((int)Session["UserID"], float.Parse(txt_moneyUpdate.Text)))
            {
                Response.Write("<script>alert('设置成功！')</script>");
                GvDataBind();
                pan_WageUpdate.Visible = false;
            }
            else
            {
                Response.Write("<script>alert('设置失败！')</script>");
            }
        }
        //设置薪资修改返回
        protected void btn_WageUpdateExit_Click(object sender, EventArgs e)
        {
            pan_WageUpdate.Visible = false;
        }
        //发工资
        protected void btn_HistoryWageSend_Click(object sender, EventArgs e)
        {
            if ((int)Session["admin"] != 2)
            {
                Response.Write("<script>alert('你还不是管理员！')</script>");
                return;
            }
            panVis();
            pan_HistoryWageSend.Visible = true;
        }
        //发工资返回
        protected void btn_HistoryWageSendExit_Click(object sender, EventArgs e)
        {
            panVis();
            pan_gzgl.Visible = true;
        }
        //确认发工资
        protected void btn_HistoryWageSendTo_Click(object sender, EventArgs e)
        {
            if (ActionDB.historyWageAdd((int)Session["UserID"], float.Parse(gv_Wage.Rows[0].Cells[1].Text), DateTime.Now))
            {
                Response.Write("<script>alert('发薪成功！')</script>");
                GvDataBind();
                panVis();
                pan_gzgl.Visible = true;
            }
            else
            {
                Response.Write("<script>alert('发薪失败！')</script>");
            }
        }
        //历史薪资退出按钮
        protected void btn_HistoryWageExit_Click(object sender, EventArgs e)
        {
            panVis();
            pan_gzgl.Visible = true;
        }
    }
}